package org.road0329.dlt698.axdr;

import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrType;

import java.nio.ByteBuffer;

// Assuming PIID, TSA, and A_ResultRecord are already defined in Java and have similar methods as in TypeScript
public class ProxyGetResponseRecord extends AxdrType {
    private ByteBuffer dataCode = null;
    private PIID piid_acd = null;
    private TSA tsa = null;
    private AResultRecord result = null;

    public ProxyGetResponseRecord() {
    }

    public void setDataCode(ByteBuffer dataCode) {
        this.dataCode = dataCode;
    }

    public void setAll(PIID piid_acd, TSA tsa, AResultRecord result) {
        this.piid_acd = piid_acd;
        this.tsa = tsa;
        this.result = result;
    }

    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        this.piid_acd = new PIID();
        this.piid_acd.setNumBit(8);
        codeLength += this.piid_acd.decode(input);
        this.tsa = new TSA();
        codeLength += this.tsa.decode(input);
        this.result = new AResultRecord();
        codeLength += this.result.decode(input);
        return codeLength;
    }

    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        int codeLength;
        if (this.dataCode != null) {
            codeLength = this.dataCode.remaining();
            byte[] data = new byte[this.dataCode.remaining()];
            this.dataCode.get(data);
            for (int i = data.length - 1; i >= 0; i--) {
                output.write(data[i]);
            }
        } else {
            if (this.result != null && this.tsa != null && this.piid_acd != null) {
                codeLength = 0;
                codeLength += this.result.encode(output);
                codeLength += this.tsa.encode(output);
                codeLength += this.piid_acd.encode(output);
            } else {
                throw new RuntimeException("piid_acd || result || tsa is null");
            }
        }
        return codeLength;
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = ByteBuffer.wrap(revOStream.getArray());
    }

    @Override
    public String toString() {
        if (this.piid_acd != null && this.result != null && this.tsa != null) {
            return "sequence: {" + "piid_acd: " + this.piid_acd + ", tsa: " + this.tsa + ", result: " + this.result + "}";
        } else {
            return "piid_acd || result || tsa is null";
        }
    }
}



